<?php

namespace App\Model;

use App\Tool\Password;
use function foo\func;

/**
 * App\Model\Admin
 *
 * @property int $id
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property string $username 用户名
 * @property string $password 密码
 * @property string $nick_name 昵称
 * @property string $head 头像
 * @property int $last_login_time 上一次登录时间
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Admin newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Admin newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Admin query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Admin whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Admin whereHead($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Admin whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Admin whereLastLoginTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Admin whereNickName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Admin wherePassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Admin whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Admin whereUsername($value)
 * @mixin \Eloquent
 * @property string|null $email 邮箱
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Admin whereEmail($value)
 * @property-read \App\Model\RulesGroupDetail $group_detail
 * @property-read \App\Model\RulesGroup $role
 */
class Admin extends Base
{
    //

    protected $table='admins';

    public $timestamps=true;

    protected $hidden=['password'];

    protected $fillable=['username','password','nick_name','head','last_login_time','email'];

    function setPasswordAttribute($value){


        $this->attributes['password']=Password::getPassword($value);
    }



    function getLastLoginTimeAttribute($value){


        return date('Y-m-d H:m:s',$value);
    }


    function group_detail(){

        return $this->hasOne(RulesGroupDetail::class,'uid','id');
    }

    function role(){

        return $this->hasOneThrough(
            RulesGroup::class,
            RulesGroupDetail::class,
            'uid',
            'id',
            'id',
            'rules_group_id'
        );
    }


    static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub

        static::deleting(function ($model){

            if($model->username=='root') throw new \Exception('初始管理员无法删除！');

        });

    }


}
